package 网络编程.综合练习.上传文件;

import java.io.*;
import java.net.Socket;

public class 客户端 {
    public static void main(String[] args) throws IOException {
        //1.创建客户端的连接通道
        Socket socket=new Socket("127.0.0.1",10086);

        //2.从连接通道获取输出流
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());

        //3.创建要发送文件的输入流
        FileInputStream fis=new FileInputStream("F\\all.jpg");

        //4.发送数据
        //创建读取的字节数组
        byte[] bytes=new byte[1024];
        int len;
        while ((len=fis.read(bytes))!=-1){
            //写入到连接通道中
            bos.write(bytes,0,len);
        }

        //5.标记已经发送完毕,关闭输出流
        socket.shutdownOutput();
        fis.close();

        //6.接收服务端反馈的信息
        BufferedReader bfr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        int b;
        while ((b= bfr.read())!=-1){
            System.out.print((char) b);
        }

        //7.释放资源
        socket.close();
    }
}

